package com.tistory.credit.controller;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.tistory.admin.adminmgr.auth.model.LoginUserInfo;
import com.tistory.admin.common.BaseException;
import com.tistory.admin.common.SessionManager;
import com.tistory.common.service.CommonService;
import com.tistory.common.vo.CommonCodeRes;
import com.tistory.common.vo.CommonReq;
import com.tistory.commons.utils.DateUtil;
import com.tistory.commons.utils.SessionUtil;
import com.tistory.commons.utils.StringUtil;
import com.tistory.credit.service.CreditService;
import com.tistory.credit.vo.CreditDetailInfo;
import com.tistory.credit.vo.CreditReq;
import com.tistory.credit.vo.CreditRes;
import com.tistory.login.service.LoginService;
import com.tistory.login.vo.MemberSearchInfo;

@Controller
public class CreditController {

	private static final Logger log = LoggerFactory.getLogger(CreditController.class);

	@Autowired
	private CreditService creditService;

	@Autowired
	private LoginService loginService;

	@Autowired
	private CommonService commonService;


	@RequestMapping(value="credit/creditMain.do")
	public ModelAndView creditMain(HttpServletRequest request , CommonReq req) throws SQLException{
		ModelAndView modelAndView = new ModelAndView();
		setReqParamValue(req);
		List<CreditRes> creditMainList = creditService.getCreditMainList(req);
		Integer totalCount = creditService.getCreditMainListCount(req);
		req.setTotalCount(totalCount);
		modelAndView.addObject("creditMainList", creditMainList);
		modelAndView.addObject("model", req);
		modelAndView.setViewName("credit/creditMain");


		// 권한체크
		modelAndView = SessionManager.init(request,modelAndView);
		return modelAndView;
	}	

	@RequestMapping(value="credit/todayMain.do")
	public ModelAndView todayMain(HttpServletRequest request , CommonReq req) throws SQLException{

		nvlStartEndDate(req);
		String user_id = request.getParameter("user_id");

		ModelAndView modelAndView = new ModelAndView();

		MemberSearchInfo searchInfo = new MemberSearchInfo();
		searchInfo = loginService.getMemberSearch(user_id);
		List<CommonCodeRes> commonCodeList  = commonService.getCommonCodeList("DP03");		// 카드수수료 가져오기

		List<CreditReq> cardSumList  = creditService.getCardSumList(user_id);					// 카드별 합계 가져오기
		List<CreditReq> withDrawInfoList  = creditService.getWithDrawInfoList(req);		// 출금현황 상황 가져오기

		modelAndView.addObject("model", searchInfo);
		modelAndView.addObject("vo", req);

		modelAndView.addObject("cardSumList", cardSumList);
		modelAndView.addObject("withDrawInfoList", withDrawInfoList);
		modelAndView.addObject("commonCodeList", commonCodeList);
		modelAndView.setViewName("credit/todayMain");

		// 권한체크
		modelAndView = SessionManager.init(request,modelAndView);
		return modelAndView;
	}		





	@RequestMapping(value="credit/popCreditStoreDetailInfo.do")
	public ModelAndView popCreditStoreDetailInfo(HttpServletRequest request) throws SQLException{
		// 권한체크
		SessionManager.funInit(request);

		String user_id = request.getParameter("user_id");
		//		String store_code = request.getParameter("store_code");

		ModelAndView modelAndView = new ModelAndView();

		MemberSearchInfo searchInfo = new MemberSearchInfo();
		searchInfo = loginService.getMemberSearch(user_id);
		List<CommonCodeRes> commonCodeList  = commonService.getCommonCodeList("DP03");		// 카드수수료 가져오기

		modelAndView.addObject("model", searchInfo);		
		modelAndView.addObject("commonCodeList", commonCodeList);
		modelAndView.setViewName("credit/popCreditStoreDetailInfo");
		return modelAndView;
	}		


	@RequestMapping(value="credit/popWithDrawDetailInfo.do")
	public ModelAndView popWithDrawDetailInfo(HttpServletRequest request , CommonReq req) throws Exception{
		// 권한체크
		SessionManager.funInit(request);
		nvlStartEndDateD(req,14);

		ModelAndView modelAndView = new ModelAndView();
		List<CreditDetailInfo> withDrawDetailInfoList  = creditService.getWithDrawDetailInfoList(req);		// 매출 상세 가져오기

		modelAndView.addObject("model", req);		
		modelAndView.addObject("withDrawDetailInfoList", withDrawDetailInfoList);		

		modelAndView.setViewName("credit/popWithDrawDetailInfo");
		return modelAndView;
	}		

	@RequestMapping(value="credit/popCardBillDetailInfo.do")
	public ModelAndView popCardBillDetailInfo(@RequestParam Map<String,Object> map, HttpServletRequest request , CommonReq req) throws Exception{
		// 권한체크
		SessionManager.funInit(request);
		nvlStartEndDateD(req,2);
		map.put("searchStRegDt", StringUtil.nvlStr(req.getSearchStRegDt(),DateUtil.getAddDay(-2, "yyyyMMdd")));
		map.put("searchEdRegDt", StringUtil.nvlStr(req.getSearchEdRegDt(),DateUtil.getSysDate("yyyyMMdd")));


		ModelAndView modelAndView = new ModelAndView();
		List<CreditReq> cardBillDetailInfoList  = creditService.getCardBillDetailInfoList(map);		// 카드사입금내역 상세조회
		MemberSearchInfo memberInfo = new MemberSearchInfo();
		memberInfo = loginService.getMemberSearch(map.get("user_id").toString());

		modelAndView.addObject("model", req);		
		modelAndView.addObject("memberInfo", memberInfo);

		int billCount = cardBillDetailInfoList.size() / 10;
		modelAndView.addObject("cardBillDetailInfoList", cardBillDetailInfoList);		
		modelAndView.addObject("billCount", billCount);
		modelAndView.setViewName("credit/popCardBillDetailInfo");
		return modelAndView;
	}			

	@RequestMapping(value="credit/popCardBillSeperateDetailInfo.do")
	public ModelAndView popCardBillSeperateDetailInfo(@RequestParam Map<String,Object> map, HttpServletRequest request , CommonReq req) throws Exception{
		// 권한체크
		SessionManager.funInit(request);

		ModelAndView modelAndView = new ModelAndView();
		List<CreditReq> cardBillSeperateDetailInfo  = creditService.getCardBillSeperateDetailInfo(map);		// 카드사별 승인내역 상세조회(입금일기준)

		modelAndView.addObject("model", map);		
		modelAndView.addObject("cardBillSeperateDetailInfo", cardBillSeperateDetailInfo);		

		modelAndView.setViewName("credit/popCardBillSeperateDetailInfo");
		return modelAndView;
	}				




	@RequestMapping(value="credit/popCardWithDrawDetailInfo.do")
	public ModelAndView popCardWithDrawDetailInfo(@RequestParam Map<String,Object> map, HttpServletRequest request) throws Exception{
		// 권한체크
		SessionManager.funInit(request);

		ModelAndView modelAndView = new ModelAndView();
		List<CreditReq> cardWithDrawDetailInfo  = creditService.getCardWithDrawDetailInfo(map);		// 카드사별 매출 상세 가져오기

		modelAndView.addObject("model", map);		
		modelAndView.addObject("cardWithDrawDetailInfo", cardWithDrawDetailInfo);		

		modelAndView.setViewName("credit/popCardWithDrawDetailInfo");
		return modelAndView;
	}		

	@RequestMapping(value="credit/popCardFeeDetailInfo.do")
	public ModelAndView popCardFeeDetailInfo(@RequestParam Map<String,Object> map, HttpServletRequest request , CommonReq req) throws Exception{
		// 권한체크
		SessionManager.funInit(request);
		nvlStartEndDateD(req,8);
		map.put("searchStRegDt", StringUtil.nvlStr(req.getSearchStRegDt(),DateUtil.getAddDay(-8, "yyyyMMdd")));
		map.put("searchEdRegDt", StringUtil.nvlStr(req.getSearchEdRegDt(),DateUtil.getSysDate("yyyyMMdd")));
		ModelAndView modelAndView = new ModelAndView();
		
		List<CreditReq> cardFeeDetailInfo  = creditService.getCardFeeDetailInfo(map);		// 카드사별 매출 상세 가져오기		
		
		modelAndView.addObject("model", req);		
		modelAndView.addObject("cardFeeDetailInfo", cardFeeDetailInfo);		

		modelAndView.setViewName("credit/popCardFeeDetailInfo");
		return modelAndView;
	}	

	@RequestMapping(value="credit/popCardOverDetailInfo.do")
	public ModelAndView popCardOverDetailInfo(@RequestParam Map<String,Object> map, HttpServletRequest request , CommonReq req) throws Exception{
		// 권한체크
		SessionManager.funInit(request);
		nvlStartEndDateD(req,3);
		map.put("searchStRegDt", StringUtil.nvlStr(req.getSearchStRegDt(),DateUtil.getAddDay(-3, "yyyyMMdd")));
		map.put("searchEdRegDt", StringUtil.nvlStr(req.getSearchEdRegDt(),DateUtil.getSysDate("yyyyMMdd")));
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("model", req);


		modelAndView.setViewName("credit/popCardOverDetailInfo");
		return modelAndView;
	}	

	@RequestMapping(value="credit/popCardGasuDetailInfo.do")
	public ModelAndView popCardGasuDetailInfo(@RequestParam Map<String,Object> map, HttpServletRequest request , CommonReq req) throws Exception{
		// 권한체크
		SessionManager.funInit(request);
		nvlStartEndDateD(req,3);
		map.put("searchStRegDt", StringUtil.nvlStr(req.getSearchStRegDt(),DateUtil.getAddDay(-3, "yyyyMMdd")));
		map.put("searchEdRegDt", StringUtil.nvlStr(req.getSearchEdRegDt(),DateUtil.getSysDate("yyyyMMdd")));
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("model", req);


		modelAndView.setViewName("credit/popCardGasuDetailInfo");
		return modelAndView;
	}	

	@RequestMapping(value="credit/popCardRealDetailInfo.do")
	public ModelAndView popCardRealDetailInfo(@RequestParam Map<String,Object> map, HttpServletRequest request , CommonReq req) throws Exception{
		// 권한체크
		SessionManager.funInit(request);
		nvlStartEndDateD(req,3);
		map.put("searchStRegDt", StringUtil.nvlStr(req.getSearchStRegDt(),DateUtil.getAddDay(-3, "yyyyMMdd")));
		map.put("searchEdRegDt", StringUtil.nvlStr(req.getSearchEdRegDt(),DateUtil.getSysDate("yyyyMMdd")));
		ModelAndView modelAndView = new ModelAndView();
		
		modelAndView.addObject("model", req);
		map.put("flagCd", "10");
		List<CreditReq> minusListDetailInfo  = creditService.getMinusListDetailInfo(map);		// 가맹점 마이너스 지급 상세내역 
		
		modelAndView.addObject("minusListDetailInfo", minusListDetailInfo);		
		modelAndView.setViewName("credit/popCardRealDetailInfo");
		return modelAndView;
	}	

	@RequestMapping(value="credit/popCardLoanDetailInfo.do")
	public ModelAndView popCardLoanDetailInfo(@RequestParam Map<String,Object> map, HttpServletRequest request , CommonReq req) throws Exception{
		// 권한체크
		SessionManager.funInit(request);

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("model", req);
		CreditReq minusDetailInfo  = creditService.getMinusDetailInfo(map);
		modelAndView.addObject("minusDetailInfo", minusDetailInfo);		
		modelAndView.setViewName("credit/popCardLoanDetailInfo");
		return modelAndView;
	}	
	
	
	@RequestMapping(value="credit/popMinusListDetailInfo.do")
	public ModelAndView popMinusListDetailInfo(@RequestParam Map<String,Object> map, HttpServletRequest request , CommonReq req) throws Exception{
		// 권한체크
		SessionManager.funInit(request);

		ModelAndView modelAndView = new ModelAndView();
		List<CreditReq> minusListDetailInfo  = creditService.getMinusListDetailInfo(map);		// 가맹점 마이너스 상세내역 

		modelAndView.addObject("model", map);		
		modelAndView.addObject("minusListDetailInfo", minusListDetailInfo);		
		modelAndView.setViewName("credit/popMinusListDetailInfo");
		return modelAndView;
	}	
	
	

	@RequestMapping(value="credit/creditNonTax.do")
	public ModelAndView creditNonTax(HttpServletRequest request, CommonReq req) throws SQLException{


		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("model", req);

		modelAndView.setViewName("credit/creditNonTax");

		// 권한체크
		modelAndView = SessionManager.init(request,modelAndView);
		return modelAndView;
	}	


	@RequestMapping(value="credit/creditMinusLoan.do")
	public ModelAndView creditMinusLoan(HttpServletRequest request,CommonReq req) throws SQLException{

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("model", req);


		modelAndView.setViewName("credit/creditMinusLoan");

		// 권한체크
		modelAndView = SessionManager.init(request,modelAndView);
		return modelAndView;
	}	

	// 예정일 업데이트
	@RequestMapping(value="credit/ajaxChangeData.do", method=RequestMethod.POST)
	public @ResponseBody String ajaxChangeData(@RequestParam Map<String,Object> map, HttpServletResponse response, HttpServletRequest request) throws SQLException, UnsupportedEncodingException{
		// 권한체크
		SessionManager.funInit(request);
		String result ="success";
		try {
			creditService.updateChangeData(map);
		} catch (Exception e) {
			e.printStackTrace();
			result = "fail";
			return result;
		}
		return result;
	}	
	
	


	private CommonReq setReqParamValue(CommonReq req){
		int listCount = 10;		
		req.setStartRow(listCount * (req.getPage()-1)) ;
		req.setEndRow(listCount);


		return req;
	}


	private void nvlStartEndDate(CommonReq req){
		if(StringUtil.isEmpty(req.getSearchStRegDt()) || StringUtil.isEmpty(req.getSearchEdRegDt())) {
			req.setSearchStRegDt(StringUtil.nvlStr(req.getSearchStRegDt(),DateUtil.getSysDate("yyyy-MM-dd")));
			req.setSearchEdRegDt(StringUtil.nvlStr(req.getSearchEdRegDt(),DateUtil.getSysDate("yyyy-MM-dd")));
		}
	};

	private void nvlStartEndDateD(CommonReq req, int kk) throws Exception{
		if(StringUtil.isEmpty(req.getSearchStRegDt()) || StringUtil.isEmpty(req.getSearchEdRegDt())) {
			req.setSearchStRegDt(DateUtil.dashDate(StringUtil.nvlStr(req.getSearchStRegDt(),DateUtil.getAddDay(-kk, "yyyy-MM-dd"))));
			req.setSearchEdRegDt(StringUtil.nvlStr(req.getSearchEdRegDt(),DateUtil.getSysDate("yyyy-MM-dd")));
		}
	};

}
